﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Kinect;
using Forms = System.Windows.Forms;
using System.Windows;
namespace Mouse_and_PowerPoint_Control
{
    public class PowerpointControl
    {
        private bool powerpointStarted = false;
        private bool bothHandsAlreadyAbove = false;
        private HandWatcher rightHandWatcher;
        private HandWatcher leftHandWatcher;

        public PowerpointControl()
        {
            rightHandWatcher = new HandWatcher("{RIGHT}", 325, 150, "left");
            leftHandWatcher = new HandWatcher("{LEFT}", 100, 250, "right");
        }

        public void DetermineAction(Point leftHandPoint, Point rightHandPoint)
        {
            startOrClosePowerPoint(leftHandPoint, rightHandPoint);

            if (powerpointStarted)
            {
                rightHandWatcher.DetermineHandAction(rightHandPoint);
                leftHandWatcher.DetermineHandAction(leftHandPoint);
            }
        }

        private void startOrClosePowerPoint(Point leftHandPoint, Point rightHandPoint)
        {
            if (leftHandPoint.Y < 50 && rightHandPoint.Y < 50)
            {
                if (!bothHandsAlreadyAbove)
                {
                    if (!powerpointStarted)
                    {
                        // Press f5
                        Forms.SendKeys.SendWait("{F5}");
                        powerpointStarted = true;
                        bothHandsAlreadyAbove = true;
                    }
                    else
                    {
                        Forms.SendKeys.SendWait("{ESC}");
                        powerpointStarted = false;
                        bothHandsAlreadyAbove = true;
                    }
                }
            }
            else
            {
                bothHandsAlreadyAbove = false;
            }
        }

        private class HandWatcher
        {
            private bool startTimer = false;
            private int timer = 0;
            private string keyboardAction;
            private string swipeDirection;
            private int startingThreshold;
            private int endingThreshold;

            public HandWatcher(string keyboardAction, int startingThreshold, int endingThreshold, string swipeDirection)
            {
                this.keyboardAction = keyboardAction;
                this.startingThreshold = startingThreshold;
                this.endingThreshold = endingThreshold;
                this.swipeDirection = swipeDirection;
            }

            public void DetermineHandAction(Point handPoint)
            {
                if (startTimer)
                {
                    timer++;
                }

                // Right hand / slide forward
                if (determineUpperThreshold(handPoint))
                {
                    if (!startTimer)
                    {
                        startTimer = true;
                    }
                }

                if (determineLowerThreshold(handPoint) && startTimer && timer < 50)
                {
                    Forms.SendKeys.SendWait(keyboardAction);
                    startTimer = false;
                    timer = 0;
                }

                if (timer >= 50)
                {
                    startTimer = false;
                    timer = 0;
                }
            }

            private bool determineUpperThreshold(Point handPoint)
            {
                if (swipeDirection == "left")
                    return handPoint.X > startingThreshold;
                else
                    return handPoint.X <= startingThreshold;
            }

            private bool determineLowerThreshold(Point handPoint)
            {
                if (swipeDirection == "left")
                    return handPoint.X < endingThreshold;
                else
                    return handPoint.X >= endingThreshold;
            }
        }
    }
}
